/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年5月27日
 */
package com.massyframework.beanskin.launcher.deploying;

import java.io.File;
import java.nio.channels.FileLock;
import java.nio.file.Path;
import java.util.Objects;

import com.massyframework.beanskin.ModuleIdentifier;
import com.massyframework.beanskin.util.localstore.LocalStore;

/**
 * 缺省的本地存储目录
 * @author huangkh
 *
 */
final class DefaultLocalStore implements LocalStore {

	private static final String CONFIG     = "config";
	private static final String DEPLOY     = "deploy";
	private static final String RUNTIME    = "runtime";
	
	private Path root;
	/* 片段资源目录 */
	private Path fragment;
	/* 前端资源目录 */
	private Path frontend;
	/* 文件锁，避免进程竞争 */
	private FileLock lock;
	
	/**
	 * 
	 */
	public DefaultLocalStore(Path root, Path fragment, Path frontend, FileLock lock, boolean clean) {
		this.root = Objects.requireNonNull(root, "\"root\" cannot be null.");
		this.fragment = Objects.requireNonNull(fragment, "\"fragment\" cannot be null.");
		this.frontend = Objects.requireNonNull(frontend, "\"frontend\" cannot be null.");
		this.lock = Objects.requireNonNull(lock, "\"lock\" cannot be null.");
		
		if (clean) {
			this.deleteDirectories(this.root.resolve(DEPLOY).toFile());
		}
	}

	@Override
	public Path getConfigDirectory(ModuleIdentifier module) {
		if (module == null) {
			return this.root.resolve(CONFIG);
		}
		String path = CONFIG.concat(File.separator)
				.concat(module.getGroupId()).concat(File.separator)
				.concat(module.getModuleId()).concat(File.separator)
				.concat(module.getVersion().toString());
		return this.root.resolve(path);
	}

	@Override
	public Path getDeploymentDirectory(ModuleIdentifier module) {
		if (module == null) {
			return this.root.resolve(DEPLOY);
		}
		String path = DEPLOY.concat(File.separator)
				.concat(module.getGroupId()).concat(File.separator)
				.concat(module.getModuleId()).concat(File.separator)
				.concat(module.getVersion().toString());
		return this.root.resolve(path);
	}

	@Override
	public Path getRuntimeDirectory(ModuleIdentifier module) {
		if (module == null) {
			return this.root.resolve(RUNTIME);
		}
		String path = RUNTIME.concat(File.separator)
				.concat(module.getGroupId()).concat(File.separator)
				.concat(module.getModuleId()).concat(File.separator)
				.concat(module.getVersion().toString());
		return this.root.resolve(path);
	}
	
	@Override
	public Path getFragmentDirectory() {
		return this.fragment;
	}

	@Override
	public Path getFrontEndDirectory() {
		return this.frontend;
	}
	
	public FileLock getFileLock() {
		return this.lock;
	}
	
	/**
	 * 递归删除目录
	 * @param directory {@link File}
	 */
	protected void deleteDirectories(File directory) {
		File[] files = directory.listFiles();
		if (files == null) return;
		
		for (File file: files) {
			if (file.isFile()) {
				file.delete();
			}else {
				this.deleteDirectories(file);
			}
		}
		
		directory.delete();
	}

	@Override
	public String toString() {
		return "DefaultLocalStore";
	}
	
}
